import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";
const billStore=createSlice({
    name:'bill',
    initialState:{
        billList:[]
    },
    reducers:{
        setBillList(state,action){
            state.billList=action.payload
        },
        addBillList(state,action){
            state.billList.push(action.payload)
        }
    }
})

const billReducer=billStore.reducer
const {setBillList,addBillList}=billStore.actions
const fetchBillList=()=>{
    return async (dispatch)=>{
        const res=await axios.get('http://localhost:31001/ka')
        dispatch(setBillList(res.data))
        
    }
}
const fetchAddBillList=(data)=>{
    return async (dispatch)=>{
        const res=await axios.post("http://localhost:31001/ka",data)
        dispatch(addBillList(res.data))
    }
}
export {setBillList,fetchBillList,fetchAddBillList}
export default billReducer